import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_oss_plugin/flutter_oss_plugin.dart';
import 'package:flutter_oss_plugin/helper/fluoss_util.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  int _progress = 0;
  FilePickerResult imgFile;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    // setState(() {
    //   _platformVersion = platformVersion;
    // });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          // child: Text('Running on: $_platformVersion\n'),
          child: ListView(
            children: [
              Container(
                  padding: EdgeInsets.all(20),
                  child: Center(
                      child: Text(
                    "name:$_platformVersion,presee:$_progress",
                    style: TextStyle(fontSize: 20),
                  ))),
              TextButton(
                onPressed: () async {
                  imgFile =
                      await FilePicker.platform.pickFiles(type: FileType.video);
                  setState(() {
                    _platformVersion = imgFile.files.first.path;
                  });
                },
                child: Text("选择图片"),
              ),
              TextButton(
                onPressed: () async {
                  await FlutterOssPlugin.getHelper().initOss(
                      endPoint: "https://oss-cn-shenzhen.aliyuncs.com",
                      stsServer:
                          "https://app.jiakeyunjs.com/javaapi/file/appGetSTSToken");
                  await FlutterOssPlugin.getHelper().upLoadFileByPath(
                      "yueyetu-video", "video/test", _platformVersion,
                      isSign: true,
                      ossUploadListener: OssUploadListener(
                          clientError: (String value) {
                            print("clientError:$value");
                          },
                          serverErrorCallBack: (ServerError value) {},
                          upLoadComplete: (String value) {
                            print("上传成功$value");
                          },
                          uploadProgressCallBack: (double value) {
                            _progress = (value).round();
                            setState(() {});
                            print("上传进度：$_progress");
                          },
                          cancelTask: (List<Map> cancel) {
                            cancel.forEach((element) {
                              print("element$element");
                            });
                          }));
                },
                child: Text("调用上传"),
              ),
              TextButton(
                  onPressed: () {
                    FlutterOssPlugin.getHelper().cancelTask(cancelAll: true);
                    print("hi hi");
                  },
                  child: Text("hi")),
              TextButton(
                  onPressed: () async {
                    await FlutterOssPlugin.getHelper().initOss(
                        endPoint: "https://oss-cn-shenzhen.aliyuncs.com",
                        stsServer:
                            "https://app.jiakeyunjs.com/javaapi/file/appGetSTSToken");
                    String videoUrl = await FlutterOssPlugin.getHelper()
                        .getFilePath("yueyetu-video", "video/test", true);
                    print("视频数据中心$videoUrl");
                  },
                  child: Text("getPathByName")),
            ],
          ),
        ),
      ),
    );
  }
}
